﻿// Decompiled with JetBrains decompiler
// Type: LanternTV.Common.Log
// Assembly: LanternTV, Version=1.4.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 9F41388C-93EF-430D-98E4-341AA823BE82
// Assembly location: F:\LanternTV\LanternTV.exe

using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Reflection;
using System.Threading;

namespace SocketioTest.Common
{
  internal class Log
  {
    private static TextWriter tWriter = (TextWriter) null;
    private static readonly object locker = new object();

    public static void Open()
    {
      try
      {
        string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "demolog\\";
        Directory.CreateDirectory(path);
        Log.tWriter = TextWriter.Synchronized((TextWriter) new StreamWriter(path + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff") + ".log"));
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.ToString());
      }
    }

    public static void Close()
    {
      if (Log.tWriter == null)
        return;
      Log.tWriter.Flush();
      Log.tWriter.Close();
      Log.tWriter = (TextWriter) null;
    }

    public static void I(string content)
    {
      MethodBase method = new StackTrace(true).GetFrame(1).GetMethod();
      Log.write("" + method.DeclaringType.FullName + "." + method.Name, content);
    }

    public static void E(string content)
    {
      MethodBase method = new StackTrace(true).GetFrame(1).GetMethod();
      Log.write("" + method.DeclaringType.FullName + "." + method.Name, content);
    }

    private static void write(string parentMethod, string content)
    {
      string str = string.Format("[{0}][{1}], [{2}], [{3}]", (object) Thread.CurrentThread.ManagedThreadId, (object) DateTime.Now.ToString("MM-dd HH:mm:ss.fff"), (object) parentMethod, (object) content);
      Console.WriteLine(str);
      if (Log.tWriter == null)
        return;
      Log.tWriter.WriteLine(str);
    }

    public static void WriteLog(string logstring)
    {
      lock (Log.locker)
      {
        string path1 = AppDomain.CurrentDomain.BaseDirectory + "Logs";
        if (!Directory.Exists(path1))
          Directory.CreateDirectory(path1);
        object[] objArray = new object[8];
        objArray[0] = (object) path1;
        objArray[1] = (object) "\\";
        DateTime now = DateTime.Now;
        objArray[2] = (object) now.Year;
        objArray[3] = (object) "-";
        now = DateTime.Now;
        objArray[4] = (object) now.Month;
        objArray[5] = (object) "-";
        now = DateTime.Now;
        objArray[6] = (object) now.Day;
        objArray[7] = (object) "_Log.log";
        string path2 = string.Concat(objArray);
        if (!System.IO.File.Exists(path2))
        {
          FileStream fileStream = System.IO.File.Create(path2);
          fileStream.Close();
          fileStream.Dispose();
        }
        using (StreamWriter streamWriter1 = new StreamWriter(path2, true))
        {
          StreamWriter streamWriter2 = streamWriter1;
          now = DateTime.Now;
          string str = "当前时间：" + now.ToString();
          streamWriter2.WriteLine(str);
          streamWriter1.WriteLine("信息：" + logstring);
          streamWriter1.WriteLine();
        }
      }
    }

    public static void WriteLog(Exception ex)
    {
      lock (Log.locker)
      {
        string path1 = AppDomain.CurrentDomain.BaseDirectory + "Logs";
        if (!Directory.Exists(path1))
          Directory.CreateDirectory(path1);
        object[] objArray = new object[8];
        objArray[0] = (object) path1;
        objArray[1] = (object) "\\";
        DateTime now = DateTime.Now;
        objArray[2] = (object) now.Year;
        objArray[3] = (object) "-";
        now = DateTime.Now;
        objArray[4] = (object) now.Month;
        objArray[5] = (object) "-";
        now = DateTime.Now;
        objArray[6] = (object) now.Day;
        objArray[7] = (object) "_Log.log";
        string path2 = string.Concat(objArray);
        if (!System.IO.File.Exists(path2))
        {
          FileStream fileStream = System.IO.File.Create(path2);
          fileStream.Close();
          fileStream.Dispose();
        }
        using (StreamWriter streamWriter1 = new StreamWriter(path2, true))
        {
          StreamWriter streamWriter2 = streamWriter1;
          now = DateTime.Now;
          string str = "当前时间：" + now.ToString();
          streamWriter2.WriteLine(str);
          streamWriter1.WriteLine("异常信息：" + ex.Message);
          streamWriter1.WriteLine("异常对象：" + ex.Source);
          streamWriter1.WriteLine("调用堆栈：\n" + ex.StackTrace.Trim());
          streamWriter1.WriteLine("触发方法：" + (object) ex.TargetSite);
          streamWriter1.WriteLine();
        }
      }
    }

    public static void WriteLog(WebException ex)
    {
      lock (Log.locker)
      {
        string path1 = AppDomain.CurrentDomain.BaseDirectory + "Logs";
        if (!Directory.Exists(path1))
          Directory.CreateDirectory(path1);
        object[] objArray = new object[8];
        objArray[0] = (object) path1;
        objArray[1] = (object) "\\";
        DateTime now = DateTime.Now;
        objArray[2] = (object) now.Year;
        objArray[3] = (object) "-";
        now = DateTime.Now;
        objArray[4] = (object) now.Month;
        objArray[5] = (object) "-";
        now = DateTime.Now;
        objArray[6] = (object) now.Day;
        objArray[7] = (object) "_Log.log";
        string path2 = string.Concat(objArray);
        if (!System.IO.File.Exists(path2))
        {
          FileStream fileStream = System.IO.File.Create(path2);
          fileStream.Close();
          fileStream.Dispose();
        }
        using (StreamWriter streamWriter1 = new StreamWriter(path2, true))
        {
          StreamWriter streamWriter2 = streamWriter1;
          now = DateTime.Now;
          string str = "当前时间：" + now.ToString();
          streamWriter2.WriteLine(str);
          streamWriter1.WriteLine("异常信息：" + ex.Message);
          streamWriter1.WriteLine("异常对象：" + ex.Source);
          streamWriter1.WriteLine("调用堆栈：\n" + ex.StackTrace.Trim());
          streamWriter1.WriteLine("触发方法：" + (object) ex.TargetSite);
          streamWriter1.WriteLine();
        }
      }
    }
  }
}
